//
//  WYMomentVideo.m
//  FriendCircleDemo
//
//  Created by mac on 2018/9/25.
//  Copyright © 2018年 WY. All rights reserved.
//

#import "WYMomentVideo.h"
#import <AVFoundation/AVFoundation.h>
@implementation WYMomentVideo
-(void)setFileName:(NSString *)fileName
{
    _fileName = [fileName copy];
    if (fileName == nil || fileName.length == 0) return ;
    
    NSString *urlStr = [[NSBundle mainBundle]pathForResource:fileName ofType:nil];
    self.playUrl = [NSURL fileURLWithPath:urlStr];
    /// 获取视频第一帧
    self.coverImage = [WYMomentVideo wy_thumbnailImageForVideo:self.playUrl atTime:1];
}

+ (UIImage*)wy_thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time {
    
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];
    NSParameterAssert(asset);
    AVAssetImageGenerator *assetImageGenerator =[[AVAssetImageGenerator alloc] initWithAsset:asset];
    assetImageGenerator.appliesPreferredTrackTransform = YES;
    assetImageGenerator.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels;
    
    CGImageRef thumbnailImageRef = NULL;
    CFTimeInterval thumbnailImageTime = time;
    NSError *thumbnailImageGenerationError = nil;
    thumbnailImageRef = [assetImageGenerator copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60)actualTime:NULL error:&thumbnailImageGenerationError];
    
    if(!thumbnailImageRef)
        NSLog(@"thumbnailImageGenerationError %@",thumbnailImageGenerationError);
    
    UIImage*thumbnailImage = thumbnailImageRef ? [[UIImage alloc]initWithCGImage: thumbnailImageRef] : nil;
    
    return thumbnailImage;
}
@end
